package com.abc.recycling.mapper;

import com.abc.recycling.domain.Message;
import org.apache.ibatis.annotations.*;
import java.time.LocalDateTime;
import java.util.List;

@Mapper
public interface MessageMapper {
    
    @Insert("INSERT INTO message (title, content, sender_id, receiver_id, type, status, create_time) " +
            "VALUES (#{title}, #{content}, #{senderId}, #{receiverId}, #{type}, #{status}, #{createTime})")
    @Options(useGeneratedKeys = true, keyProperty = "id")
    int insert(Message message);
    
    @Update("UPDATE message SET status = 1, update_time = NOW() WHERE id = #{id}")
    int markAsRead(Long id);
    
    @Select("SELECT * FROM message WHERE id = #{id}")
    Message findById(Long id);
    
    @Select("SELECT * FROM message WHERE receiver_id = #{userId} ORDER BY create_time DESC")
    List<Message> findByUserId(Long userId);
    
    @Select("SELECT * FROM message WHERE receiver_id = #{userId} AND status = #{status}")
    List<Message> findByUserIdAndStatus(@Param("userId") Long userId, @Param("status") int status);
    
    @Select("SELECT COUNT(*) FROM message WHERE receiver_id = #{userId} AND status = 0")
    int countUnread(Long userId);
    
    @Delete("DELETE FROM message WHERE id = #{id}")
    int deleteById(Long id);
    
    /**
     * 根据条件搜索消息
     */
    @Select("<script>" +
            "SELECT * FROM message " +
            "WHERE receiver_id = #{userId} " +
            "<if test='type != null'> AND type = #{type} </if> " +
            "<if test='status != null'> AND status = #{status} </if> " +
            "<if test='keyword != null and keyword != \"\"'>" +
            "   AND (title LIKE CONCAT('%', #{keyword}, '%') " +
            "        OR content LIKE CONCAT('%', #{keyword}, '%'))" +
            "</if> " +
            "<if test='startTime != null'> AND create_time &gt;= #{startTime} </if> " +
            "<if test='endTime != null'> AND create_time &lt;= #{endTime} </if> " +
            "ORDER BY create_time DESC" +
            "</script>")
    List<Message> search(@Param("userId") Long userId,
                        @Param("type") String type,
                        @Param("status") Integer status,
                        @Param("keyword") String keyword,
                        @Param("startTime") LocalDateTime startTime,
                        @Param("endTime") LocalDateTime endTime);
    
    /**
     * 批量删除消息
     */
    @Delete("<script>" +
            "DELETE FROM message WHERE id IN " +
            "<foreach collection='ids' item='id' open='(' separator=',' close=')'>" +
            "   #{id}" +
            "</foreach>" +
            "</script>")
    int deleteByIds(@Param("ids") List<Long> ids);
    
    /**
     * 批量标记消息为已读
     */
    @Update("UPDATE message SET status = 1, update_time = #{updateTime} WHERE id IN " +
            "<foreach collection='ids' item='id' open='(' separator=',' close=')'>" +
            "#{id}" +
            "</foreach>")
    int markReadByIds(@Param("ids") List<Long> ids, @Param("updateTime") LocalDateTime updateTime);
    
    @Select("SELECT COUNT(*) FROM message WHERE receiver_id = #{userId} AND status = #{status}")
    int countByUserIdAndStatus(@Param("userId") Long userId, @Param("status") int status);
    
    @Update("UPDATE message SET status = #{status}, update_time = #{updateTime} WHERE id = #{id}")
    int updateStatus(@Param("id") Long id, @Param("status") int status, @Param("updateTime") LocalDateTime updateTime);
} 